<?php
namespace app\admin\controller;
use app\model\Integral as IntegralModel;
use think\Db;


class Integral extends Base
{
    public function __construct(){
        $model = new IntegralModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '活动';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function assignFields(){
        $model = $this->model;

        $users = Db::name('user')->select();
        $users = arrayParamExtract($users,'id','nickname');

        $fields = [
            'user_id'=>['name'=>'用户','type'=>'select1','params'=>$users],
            'amount'=>['name'=>'积分（正数增加积分，负数减少积分）','type'=>'text','valids'=>['required']],
            'remark'=>['name'=>'备注','type'=>'textarea'],
            
        ];
        $valids = createPageValid($fields);
        $this->assign([
            'fields'=>$fields,
            'valids'=>$valids
        ]);

    }

    public function assignSearchs(){
        $model = $this->model;

        $users = Db::name('user')->select();
        $users = arrayParamExtract($users,'id','nickname');

        $search_params = [
            [
                ['key'=>'user_id','name'=>'用户','type'=>'select1_1','params'=>$users],
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);
    }

    public function index(){
        $model = $this->model;
        $map_params = [
            ['key'=>'user_id','type'=>'='],
        ];
        $map = getSearchParam($map_params);

        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',20)
        ];
        // 显示筛选
        $this->assignSearchs();

        $list = $model->where($map)->order('id','desc')->paginate($page_param);
        $page = $list->render();
        $list = $list->toarray();
        foreach ($list['data'] as $k => $vo) {
            $list['data'][$k]['nickname'] = Db::name('user')->where('id',$vo['user_id'])->value('nickname');
        }
        return view('index', [
            'list'=>$list,
            'page'=>$page
        ]);
    }

    public function save(){
        $model = $this->model;
        if (request()->isPost()) {
            $fields = [
                'must'=>['user_id','amount'],
                'nomust'=>['remark']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
            $save_data['type'] = 10;
            if ( $model->saveData($save_data) ) {
                $this->success('操作成功',url('index',['user_id'=>$save_data['user_id']]));
            }else{
                $this->error('操作失败，请刷新重试');
            }
        }

        $this->assignFields();
        $data = [];
        $data['user_id'] = input('user_id',0);
        return view(emptyDef($this->page_info['view'],'common/save'), [
            'data'=>$data
        ]);
    }





   
}
